# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libssh2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-wincng")
pkgver=1.10.0
pkgrel=1
pkgdesc="A library implementing the SSH2 protocol as defined by Internet Drafts (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://libssh2.org/"
license=("BSD")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('staticlibs' 'strip')
source=("https://libssh2.org/download/${_realname}-${pkgver}.tar.gz"{,.asc}
        fix-pkgconfig.patch)
sha256sums=('2d64e90f3ded394b91d3a2e774ca203a4179f69aebee03003e5a6fa621e41d51'
            'SKIP'
            '7a6545f6d457ad008aacefe04a60727c02d33927c8a903745bf191f69cc8ba55')
validpgpkeys=('27EDEAF22F3ABCEB50DB9A125CC908FDB71E12C2'   # Daniel Stenberg
              '914C533DF9B2ADA2204F586D78E11C6B279D5C91')  # Daniel Stenberg (old key)

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/fix-pkgconfig.patch
  autoreconf -fi
}

do_build() {
  builddir="${srcdir}/build-${CARCH}-$1"
  [[ -d "$builddir" ]] && rm -rf "${builddir}"
  mkdir -p "${builddir}" && cd "${builddir}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-crypto=$1

  make
}

build() {
  do_build openssl
  do_build wincng
}

check() {
  cd "${srcdir}/build-${CARCH}-openssl"
  make check || true

  cd "${srcdir}/build-${CARCH}-wincng"
  make check || true
}

do_package() {
  cd "${srcdir}/build-${CARCH}-$1"
  make DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_libssh2() {
  depends=("${MINGW_PACKAGE_PREFIX}-openssl"
          "${MINGW_PACKAGE_PREFIX}-zlib")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-wincng")
  do_package openssl
}

package_libssh2-wincng() {
  depends=("${MINGW_PACKAGE_PREFIX}-zlib")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  do_package wincng
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
